package com.acoffee.service.Impl;

import com.acoffee.mapper.GoodsMapper;
import com.acoffee.pojo.Goods;
import com.acoffee.pojo.PageBean;
import com.acoffee.service.GoodsService;
import com.acoffee.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author acoffee
 * @create 2021-10-28 16:46
 */
public class GoodsServiceImpl implements GoodsService {

    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
    @Override
    public List<Goods> queryGoodsIsNew() {

        List<Goods> goods = goodsMapper.queryGoodsIsNew();
        return goods;
    }

    @Override
    public List<Goods> queryGoodsIsHot() {
        List<Goods> goods = goodsMapper.queryGoodsIsHot();

        return goods;
    }

    @Override
    public Goods details(int id) {
        Goods goods = goodsMapper.queryById(id);
        return goods;
    }

    @Override
    public PageBean<Goods> queryPageByCid(int cid, int currentPage) {

        PageBean<Goods> pageBean = new PageBean<>();

        //设置当前页码
        pageBean.setCurrentPage(currentPage);

        //设置每页数量
        pageBean.setTotalPage(12);

        //设置总商品数
        int count = goodsMapper.queryCountByCid(cid);
        pageBean.setTotalCount(count);

        //设置总页数
        int totalPage = pageBean.getTotalCount()/pageBean.getTotalPage();
        if(pageBean.getTotalCount() % pageBean.getTotalPage() > 0){
            totalPage += 1;
        }
        pageBean.setTotalPage(totalPage);

        //这只每页展示的商品
        List<Goods> list = goodsMapper.queryPageByCid(cid,(currentPage - 1)*12);
        pageBean.setItems(list);


        return pageBean;
    }

    @Override
    public List<Goods> queryRankGoods(int cid) {
        List<Goods> goods = goodsMapper.queryRankGoods(cid);

        return goods;
    }

}
